package xyf.p02.stack_queue;

import java.util.LinkedList;

/**
 * ClassName: M0306_AnimalShelf
 * Description:
 * Author: xyf
 * Date: 9/7/22 7:48 PM
 * Version: 1.0
 **/
public class M0306_AnimalShelf
{
    class AnimalShelf
    {

        private LinkedList<Integer> dogQueue = new LinkedList<>();

        private LinkedList<Integer> catQueue = new LinkedList<>();

        public AnimalShelf()
        {

        }

        public void enqueue(int[] animal)
        {
            if (animal[1] == 0)
            {
                catQueue.offer(animal[0]);
            }
            else if (animal[1] == 1)
            {
                dogQueue.offer(animal[0]);
            }
        }

        public int[] dequeueAny()
        {
            int[] res = new int[2];
            if (catQueue.isEmpty() && dogQueue.isEmpty())
            {
                res[0] = -1;
                res[1] = -1;
                return res;
            }

            if (catQueue.isEmpty())
            {
                return dequeueDog();
            }

            if (dogQueue.isEmpty())
            {
                return dequeueCat();
            }

            Integer cat = catQueue.peek();
            Integer dog = dogQueue.peek();
            if (cat < dog)
            {
                cat = catQueue.poll();
                res[0] = cat;
                res[1] = 0;
                return res;
            }
            else
            {
                dog = dogQueue.poll();
                res[0] = dog;
                res[1] = 1;
                return res;
            }
        }

        public int[] dequeueDog()
        {
            int[] res = new int[2];
            if (dogQueue.isEmpty())
            {
                res[0] = -1;
                res[1] = -1;
                return res;
            }
            Integer dog = dogQueue.poll();
            res[0] = dog;
            res[1] = 1;
            return res;
        }

        public int[] dequeueCat()
        {
            int[] res = new int[2];
            if (catQueue.isEmpty())
            {
                res[0] = -1;
                res[1] = -1;
                return res;
            }
            Integer cat = catQueue.poll();
            res[0] = cat;
            res[1] = 0;
            return res;
        }
    }
}
